package com.qf.security;

import com.qf.utils.ResponseUtils;
import com.qf.utils.ResultMsg;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//处理没有权限时的方案
public class QfAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        //回复：没有权限的通知
        /*ResultMsg.faild(777,"没有权限！！");//封装的数据
        ObjectMapper objectMapper=new ObjectMapper(); //jackson提供的一个对象
        //将对象以IO形式写出；
        objectMapper.writeValue(response.getOutputStream(),ResultMsg.faild(777,"没有权限！！"));*/
        ResponseUtils.response(response, ResultMsg.ERROR(777, e.getMessage()));
    }
}
